﻿using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using Microsoft.Win32;

namespace VolgaTransTelecomClient
{
    [RunInstaller(true)]
    public class ProductInstaller : Installer
    {
        #region Methods
        /// <summary>
        /// When overridden in a derived class, completes the install transaction.
        /// </summary>
        /// <param name="savedState">An <see cref="T:System.Collections.IDictionary"/> that contains the state of the computer after all the installers in the collection have run.</param>
        /// <exception cref="T:System.ArgumentException">
        /// The <paramref name="savedState"/> parameter is null.
        /// -or-
        /// The saved-state <see cref="T:System.Collections.IDictionary"/> might have been corrupted.
        /// </exception>
        /// <exception cref="T:System.Configuration.Install.InstallException">
        /// An exception occurred during the <see cref="M:System.Configuration.Install.Installer.Commit(System.Collections.IDictionary)"/> phase of the installation. This exception is ignored and the installation continues. However, the application might not function correctly after the installation is complete.
        /// </exception>
        public override void Commit(IDictionary savedState)
        {
            base.Commit(savedState);
            Assembly executingAssembly = Assembly.GetExecutingAssembly();
            string assemblyLocation = executingAssembly.Location;
            RegistryKey registryKey = Registry.CurrentUser;
            registryKey = GetKey(registryKey, "Software");
            registryKey = GetKey(registryKey, "Microsoft");
            registryKey = GetKey(registryKey, "Windows");
            registryKey = GetKey(registryKey, "CurrentVersion");
            registryKey = GetKey(registryKey, "Run");
            registryKey.SetValue(executingAssembly.GetName().Name, assemblyLocation, RegistryValueKind.String);
            Process application = new Process();
            application.StartInfo.FileName = assemblyLocation;
            application.Start();
        }

        /// <summary>
        /// Raises the <see cref="E:System.Configuration.Install.Installer.AfterUninstall"/> event.
        /// </summary>
        /// <param name="savedState">An <see cref="T:System.Collections.IDictionary"/> that contains the state of the computer after all the installers contained in the <see cref="P:System.Configuration.Install.Installer.Installers"/> property are uninstalled.</param>
        protected override void OnAfterUninstall(IDictionary savedState)
        {
            base.OnAfterUninstall(savedState);
            Assembly executingAssembly = Assembly.GetExecutingAssembly();
            string assemblyLocation = executingAssembly.Location;
            string assemblyFolder = Path.GetDirectoryName(assemblyLocation);
            string exceptionLogFile = Path.Combine(assemblyFolder, Logger.FileName);
            File.Delete(exceptionLogFile);
            RegistryKey registryKey = Registry.CurrentUser;
            registryKey = GetKey(registryKey, "Software");
            registryKey = GetKey(registryKey, "Microsoft");
            registryKey = GetKey(registryKey, "Windows");
            registryKey = GetKey(registryKey, "CurrentVersion");
            registryKey = GetKey(registryKey, "Run");
            registryKey.DeleteValue(executingAssembly.GetName().Name);
        }

        /// <summary>
        /// Gets the key.
        /// </summary>
        /// <param name="registryKey">The registry key.</param>
        /// <param name="keyName">Name of the key.</param>
        /// <returns>Registry key.</returns>
        private static RegistryKey GetKey(RegistryKey registryKey, string keyName)
        {
            List<string> subKeys = new List<string>(registryKey.GetSubKeyNames());
            if (!subKeys.Contains(keyName))
            {
                registryKey.CreateSubKey(keyName);
            }
            return registryKey.OpenSubKey(keyName, true);
        }
        #endregion
    }
}
